package com.chezhujie.www.controller;

import com.chezhujie.www.service.GenerateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/22.
 */
@Controller
@RequestMapping("/generate")
public class GenerateController {
    @Autowired
    private GenerateService generateService;

    @RequestMapping("/createTable.do")
    public String createTable(String tablename,HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) {
        try {
            List<String> classNameList = generateService.getEntity();
            model.addAttribute("classNameList",classNameList);
            model.addAttribute("tablename",tablename);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed",e.getMessage());
        }
        return "generate/table";
    }
    @RequestMapping("/createTableSql.do")
    public String createTableSql(@RequestParam(required = true) String tablename,
                       HttpServletRequest req, HttpServletResponse resp,ModelMap model) {
        model.addAttribute("tablename", tablename);

        String sql = "";
        try {
            List<String> classNameList = generateService.getEntity();
            model.addAttribute("classNameList",classNameList);
            sql = generateService.createTableSql(tablename);
        }catch (ClassNotFoundException e) {
            model.addAttribute("msg", "生成sql失败！找不到相对应的类，请检查类("+e.getMessage()+")是否存在！");
        }catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            String reg = "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:";
            if(msg.indexOf(reg)>0){
                msg = msg.substring(msg.indexOf(reg)+reg.length());
            }
            model.addAttribute("msg", "生成sql失败！"+msg);
        }
        model.addAttribute("createTableSql", sql);

        return "generate/table";
    }
    @RequestMapping("/importSql.do")
    public String importSql(@RequestParam(required = true) String tablename,
                       HttpServletRequest req, HttpServletResponse resp,ModelMap model) throws IOException {
        Map<String, Object> map = new HashMap<String,Object>();
        model.addAttribute("tablename",tablename);
        String sql = "";
        try {
            List<String> classNameList = generateService.getEntity();
            model.addAttribute("classNameList",classNameList);
            sql = generateService.createTableSql(tablename);
            model.addAttribute("createTableSql",sql);
            generateService.update(sql);
           model.addAttribute("success","操作成功");
        }catch (ClassNotFoundException e) {
            model.addAttribute("msg", "生成sql失败！找不到相对应的类，请检查类("+e.getMessage()+")是否存在！");
        }catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            String reg = "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:";
            if(msg.indexOf(reg)>0){
                msg = msg.substring(msg.indexOf(reg)+reg.length());
            }
            model.addAttribute("msg","导入数据库失败！"+msg);
        }
        return "generate/table";
    }


    @RequestMapping("/downloadTemplate.do")
    public String downloadTemplate(@RequestParam(required = true) String tablename,
                                   HttpServletRequest req, HttpServletResponse resp,
                              ModelMap model) {
        try {
            List<String> classNameList = generateService.getEntity();
            model.addAttribute("classNameList",classNameList);
            generateService.downloadTemplate(tablename,resp);

            model.addAttribute("success", "生成代码成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "生成代码失败！");
        }


        return "generate/table";
    }


}
